<?php

/*
rpc模式
    路由模式是根据路由key进行完整匹配（完全相等才发送消息），这里的通配符模式通俗来讲就是模糊匹配
    符号‘#’表示匹配一个或多个词，符号‘*’表示匹配一个词 类似sql中的 = 和 like 的关系
*/

require_once __DIR__ . '/../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
 
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
 
$channel->queue_declare('rpc_queue', false, false, false, false);
 
function fib($n) {
    // if ($n == 0)
    //     return 0;
    // if ($n == 1)
    //     return 1;
    // return fib($n-1) + fib($n-2);
    return json_encode(['a'=>123,'b'=>456]);
    // return "aaaaa";
}
 
echo " [x] Awaiting RPC requests\n";
$callback = function($req) {
    $n = intval($req->body);
    echo " [.] fib(", $n, ")\n";
 
    $msg = new AMQPMessage(
        (string) fib($n),
        array('correlation_id' => $req->get('correlation_id'))
        );
 
    $req->delivery_info['channel']->basic_publish(
        $msg, '', $req->get('reply_to'));
    $req->delivery_info['channel']->basic_ack(
        $req->delivery_info['delivery_tag']);
};
 
$channel->basic_qos(null, 1, null);
$channel->basic_consume('rpc_queue', '', false, false, false, false, $callback);
 
while(count($channel->callbacks)) {
    $channel->wait();
}
 
$channel->close();
$connection->close();